package com.js.web.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.js.core.entity.result.ResultBean;
import com.js.core.dto.ArticleDto;
import com.js.core.dto.AttitudeDto;
import com.js.core.entity.Article;
import com.js.core.entity.ArticleAttitude;
import com.js.core.entity.ArticleContent;
import com.js.core.mapper.ArticleMapper;
import com.js.core.service.ArticleContentService;
import com.js.core.service.ArticleService;
import com.js.core.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *
 * @author knight
 * @Auther: MuYun
 * @Date: 2024/7/24 17:53
 * @Description：文章相关的接口层
 *
 */
@RequestMapping("/article")
@RestController
public class ArticleController {

    private static Logger log= LoggerFactory.getLogger(ArticleController.class);

    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleContentService articleContentService;


    /**
     * 列表查询
     * @return
     */
    @RequestMapping("/list")
    public ResultBean list(){
        ResultBean resultBean=new ResultBean();
        try {
            ResultBean articleRet = articleService.queryArticleAndContent();
            resultBean.setData(articleRet.getData());
        }catch (Exception e){
            log.error("列表接口出错：{}",e);
            resultBean.setCode(ResultBean.CODE_ERROR);
        }
        return resultBean;
    }

    /**
     * 文章查询
     * @param articleDto
     * @return
     */
    @RequestMapping("/query")
    public ResultBean query(ArticleDto articleDto){
        ResultBean resultBean=new ResultBean();
        Map<String,Object> map=new HashMap<>();
        String type=articleDto.getType();
        // 定义是否需要查询的标志
        boolean needInfo = false;
        boolean needContent = false;
        boolean needAttitude = false;
        try {
            // 根据 type 决定需要查询哪些部分
            switch (type) {
                case "full":
                    needInfo = true;
                    needContent = true;
                    needAttitude = true;
                    break;
                case "noAttitude":
                    needInfo = true;
                    needContent = true;
                    break;
                case "noContent":
                    needInfo = true;
                    needAttitude = true;
                    break;
                case "noInfo":
                    needContent = true;
                    needAttitude = true;
                    break;
                case "onlyInfo":
                    needInfo = true;
                    break;
                case "onlyContent":
                    needContent = true;
                    break;
                case "onlyAttitude":
                    needAttitude = true;
                    break;
                default:
                    throw new IllegalArgumentException("不支持的查询类型: " + type);
            }
            // 按需调用
            if (needInfo) {
                Article article = articleService.getById(articleDto.getId());
                map.put("article", article);
            }
            if (needContent) {
                QueryWrapper<ArticleContent> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("article_id", articleDto.getId());
                ArticleContent articleContent = articleContentService.getOne(queryWrapper);
                map.put("content",articleContent);
            }
            if (needAttitude) {
                ArticleAttitude articleAttitude=new ArticleAttitude();
                articleAttitude.setArticleId(articleDto.getId());
                articleAttitude.setUserId("模拟当前登陆用户id");
                ResultBean attitudeResultBean = articleService.queryAttitude(articleAttitude);
                if(ObjectUtil.isNotNull(attitudeResultBean.getData(List.class))){
                    map.put("attitude",attitudeResultBean.getData(List.class));
                }
            }
            resultBean.setData(map);
        }catch (Exception e){
            log.error("查询正文接口出错：{}",e);
            resultBean.setCode(ResultBean.CODE_ERROR);
        }
        return resultBean;
    }

    /**
     * 保存草稿/发布文章
     * @param articleDto
     * @return
     */
    @RequestMapping("/saveOrPublish")
    public ResultBean saveDraft(ArticleDto articleDto){
        ResultBean resultBean=new ResultBean();
        try{
            resultBean=articleService.saveOrPubish(articleDto);
            ArticleContent articleContent=new ArticleContent();
            articleContent.setArticleId(articleDto.getId());
            articleContent.setContent(articleDto.getContent());
            articleContent.setHtml(articleDto.getHtml());
            UpdateWrapper<ArticleContent> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("article_id", articleDto.getId());
            articleContentService.saveOrUpdate(articleContent,updateWrapper);
        }catch (Exception e){
            log.error("保存草稿接口出错：{}",e);
        }
        return resultBean;
    }

    /**
     * 对文章进行表态
     * @param attitudeDto
     * @return
     */
    @RequestMapping("/showAttitude")
    public ResultBean showAttitude(AttitudeDto attitudeDto){
        ResultBean resultBean=new ResultBean();
        try {
            switch (attitudeDto.getType()) {
                case "look":
                    resultBean = articleService.look(attitudeDto);
                    break;
                case "agree":
                case "noAgree":
                    resultBean = articleService.agree(attitudeDto);
                    break;
                case "dislike":
                case "afterDislike":
                    resultBean = articleService.dislike(attitudeDto);
                    break;
                case "collect":
                case "noCollect":
                    resultBean = articleService.collect(attitudeDto);
                    break;
                default:
                    return resultBean;
            }
        }catch (Exception e){
            log.error("接口出错：{}",e);
            resultBean.setCode(ResultBean.CODE_ERROR);
        }
        return resultBean;
    }

}
